import socket
import json
import os
import struct
sk=socket.socket()
sk.connect(('127.0.0.1',9001))
abs_path=r'D:\AK\默写\默写.txt'
filename=os.path.basename(abs_path)
filesize=os.path.getsize(abs_path)
dic={'filename':filename,'filesize':filesize}
str_dic=json.loads(dic)
b_dic=str_dic.encode('utf-8')
mlen=struct.pack('i',len(b_dic))
sk.send(mlen)
sk.send(b_dic)

#小文件读取
# with open(abs_path,mode='rb') as f:
#     content=f.read()
#     print(content,len(content))
#     sk.send(content)


#发送大文件
with open(abs_path,mode='rb') as f:
    while filesize>0:
        content=f.read(1024)
        filesize-=len(content)
        sk.send(content)


sk.close()

























